package com.asset.dispoal.business.bsEvidenceRecognition.service.commom;

import com.asset.dispoal.business.bsEvidenceRecognition.domain.db.mapper.BusinessDataManageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.redis.commands.IRedisExtCommands;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class EvidenceMatchResult3Command implements BaseCommand<Map, Result> {

    @Autowired
    private IRedisExtCommands redisExtCommands;

    @Autowired
    BusinessDataManageMapper businessDataManageMapper;

    @Autowired
    BaseIdeable<String> snowFlake;
    @Autowired
    private Environment environment;

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED, rollbackFor = Exception.class)
    public Result execute(Map map) {
        if (!map.get("code").equals(200)) {
            return Result.ok();
        }
        implement((Map) map.get("data"));
        return Result.ok();
    }

    public void implement(Map stringObjectMap) {
        String baseUrl = environment.getProperty("file.evidence.url");

        String requestKey = (String) stringObjectMap.get("requestKey");
        String responseKey = (String) stringObjectMap.get("responseKey");
        final Map resultMap = redisExtCommands.hget(6, "ocr_other_notify_key2", responseKey + "_" + requestKey, Map.class);
        log.info("resultMap:{}", resultMap);
        businessDataManageMapper.updateEvidenceRecordState(requestKey);
        //fileName 去匹配recognition表中的filepath + repalceName
        if (CollectionUtil.isNotEmpty(resultMap)) {
            List<Map<String, Object>> datas = (List<Map<String, Object>>) resultMap.get("data");
            for (Map<String, Object> data : datas) {
                String fileName = (String) data.get("filename");
                String excludeBaseUrl = null;
                if (baseUrl != null) {
                    excludeBaseUrl = fileName.replace(baseUrl, "");
                }
                String recognitionId = businessDataManageMapper.queryRecognitionIdByUrl(excludeBaseUrl);
                List<Map<String, String>> matchResults = (List<Map<String, String>>) data.get("matchResult");
                if (CollectionUtil.isNotEmpty(matchResults)) {
                    for (Map<String, String> matchResult : matchResults) {
                        businessDataManageMapper.insertEvidenceMapper(snowFlake.generateId(), recognitionId,
                                matchResult.get("matchKey"),
                                matchResult.get("matchData"));

                        businessDataManageMapper.insertEvidenceMapperRecord(snowFlake.generateId(), recognitionId,
                                matchResult.get("matchKey"),
                                matchResult.get("matchData"));
                    }
                    String evidenceTypeId = matchResults.get(0).get("association");
                    businessDataManageMapper.updateTypeAndStatusSuccess(evidenceTypeId, recognitionId);
                }
            }
        }
        redisExtCommands.hdel(6, "ocr_other_before2", requestKey);
        redisExtCommands.hdel(6, "ocr_other_data2", requestKey);
        redisExtCommands.hdel(6, "ocr_other_notify_key2", responseKey + "_" + requestKey);
    }
}
